<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Peternakan extends CI_Controller{

	function __construct()
	{
		parent::__construct();
		$this->load->model("Peternakan_model");
		$this->load->model('Home_model');
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE){
			redirect('auth');
		}else{
			$id_role = $this->session->userdata('id_role'); 
			$data = array(
				"main_view" => "peternakan/konten",
				"title"		=> "Manajemen Peternakan",
				"nama_user"	=> "Admin",
				"peternakan"=> $this->Peternakan_model->get_all_peternakan(),
				"kota"		=> $this->Peternakan_model->get_kota(),
				"hak_akses0"=> $this->Home_model->get_hak_akses($id_role,0),
				"hak_akses1"=> $this->Home_model->get_hak_akses($id_role,1),
				"hak_akses2"=> $this->Home_model->get_hak_akses($id_role,2)
			);
			//var_dump($data["peternakan"]->result_array());
			$this->load->view('template', $data);
		}
	}
	
	function tambah_peternakan()
	{
		$tgl_berdiri = $this->input->post("tgl-berdiri");
		$tgl = explode('/', $tgl_berdiri);
		if(count($tgl) > 1) $tgl_berdiri = $tgl[2]."-".$tgl[0]."-".$tgl[1];
		
		$titik_pasok = array(
			"ID_KOTA"			=> $this->input->post("kota"),
			"NAMA"				=> $this->input->post("nama"),
			"ALAMAT"			=> $this->input->post("alamat"),
			"NO_TELP"			=> $this->input->post("telepon"),
			"EMAIL"				=> $this->input->post("email"),
			"KODE_POS"			=> $this->input->post("kode-pos"),
			"TANGGAL_BERDIRI"	=> $tgl_berdiri,
			"INFO"				=> $this->input->post("info"),
			"LAT"				=> $this->input->post("lat"),
			"LON"				=> $this->input->post("lng")
		);
		$id_titik_pasok = $this->Peternakan_model->insert_titik_pasok($titik_pasok);
		$peternakan = array(
			"ID_TITIK_PASOK"	=> $id_titik_pasok,
			"JUMLAH_SAPI"		=> $this->input->post("jumlah")
		);
		if($this->Peternakan_model->insert_peternakan($peternakan)){
			redirect("/peternakan");
		}else{
			echo "gagal";
		}
		
	}
	
	function update_peternakan()
	{
		$tgl_berdiri = $this->input->post("tgl-berdiri");
		$tgl = explode('/', $tgl_berdiri);
		if(count($tgl) > 1) $tgl_berdiri = $tgl[2]."-".$tgl[0]."-".$tgl[1];
		$titik_pasok = array(
			"ID_KOTA"			=> $this->input->post("kota"),
			"NAMA"				=> $this->input->post("nama"),
			"ALAMAT"			=> $this->input->post("alamat"),
			"NO_TELP"			=> $this->input->post("telepon"),
			"EMAIL"				=> $this->input->post("email"),
			"KODE_POS"			=> $this->input->post("kode-pos"),
			"TANGGAL_BERDIRI"	=> $tgl_berdiri,
			"INFO"				=> $this->input->post("info"),
			"LAT"				=> $this->input->post("lat"),
			"LON"				=> $this->input->post("lng")
		);
		
		$id_titik_pasok = $this->input->post("id-titik-pasok");
		if($this->Peternakan_model->update_titik_pasok($id_titik_pasok, $titik_pasok)){
			$peternakan = array(
				"JUMLAH_SAPI"		=> $this->input->post("jumlah")
			);
			
			if($this->Peternakan_model->update_peternakan($id_titik_pasok, $peternakan)){
				redirect("/peternakan");
			} else {
				echo "gagal";
			}
		
		}
	}
	
	function delete_peternakan()
	{
		
		if($this->Peternakan_model->delete_peternakan($this->input->post("id_titik_pasok"))){
			if($this->Peternakan_model->delete_titik_pasok($this->input->post("id_titik_pasok"))){
				echo json_encode(true);
			} else {
				echo json_encode(false);
			}
		}else{
			echo json_encode(false);
		}
	}

}